/** 商品功能模块 */
export const goodsModuleMixin = {
	data() {
		return {
			goodsList: [],
			goodsListPage: 1,
			goodsListTotal: 0,
			goodsListAfresh: false,
			goodsListLoading: true,
			goodsListEmpty: false,
			goodsListRequestStart: false, // 请求开始
			goodsListResetTimer: null,
		}
	},
	onPullDownRefresh() { this.resetgoodsList(); },
	onReachBottom() { this.goodsListLoading && this.getgoodsList(); },
	onLoad() { this.getgoodsList(); },
	methods: {
		/** 重新获取商品列表 */
		resetgoodsList() {
			this.goodsListAfresh = true;
			this.goodsListLoading = true;
			this.goodsListEmpty = false;
			this.goodsListRequestStart = false;
			clearTimeout(this.goodsListResetTimer);
			this.goodsListResetTimer = setTimeout(() => this.getgoodsList(), 300);
		},
		/** 获取商品列表执行 */
		async getgoodsList() {
			if (this.goodsListRequestStart) return;
			this.goodsListRequestStart = true;
			if (this.goodsListAfresh) this.goodsListPage = 1;
			try {
				var res = await this.$post('6544dfc8ccf1b', {
					page: this.goodsListPage,
					list_rows: 10,
					// 其他参数
					goods_type: 0,
					order_type: 1,
					is_recommend: 1,
				});
				const _goodsList = (res.data.data || []).map((t, i) => {
					return { ...t, animationTime: i / 10 + 0.25 }
				})
				if (this.goodsListAfresh) this.goodsList = _goodsList;
				else this.goodsList = this.goodsList.concat(_goodsList);
				this.goodsListPage++;
				this.goodsListTotal = res.data.total;
				this.goodsListLoading = this.goodsList.length < this.goodsListTotal;
			} catch (e) { this.goodsListLoading = false; }
			this.goodsListAfresh = false;
			uni.stopPullDownRefresh();
			this.goodsListRequestStart = false;
			this.goodsListEmpty = !this.goodsList.length && !this.goodsListLoading;
		},
	}
};